﻿typedef struct BMPSTRUCT {   
    void * data;  //数据指针   
    int bmpWidth;  //图片宽度   
    int bmpHeight;  //图片高度   
    unsigned char bmpBiBitCount; //bmp类型 1 4 8 24 32   
}BmpDataBox;

//宏定义 用于固定的偏移   
#define DataOffset 10   
#define SizeOffset 18   
#define BiBitCount  2   
#define COLORTABLE 54 
// 读取bmp文件 (24位)
BmpDataBox* readBmp(char *BmpFileName)
{
    FILE * pFile;   
    BmpDataBox * newBmpData = NULL;   
    int dataOffset;  

    pFile = fopen(BmpFileName,"r");  

    if(!pFile)   
    {    
        return newBmpData;   
    }   
    newBmpData = (BmpDataBox *)malloc(sizeof(BmpDataBox));


    //printf("file %s open successs!\n",bmpPath);   
    //得到数据的偏移字节   
    fseek(pFile,DataOffset,SEEK_SET);   
    fread(&dataOffset,4,1,pFile);   
    //得到bmp的宽与高的像素数   
    fseek(pFile,SizeOffset,SEEK_SET);   
    fread(&newBmpData->bmpWidth,4,1,pFile);   
    fread(&newBmpData->bmpHeight,4,1,pFile);   
    //注意,有些图片存在取出为负的情况,我也没弄清楚   
    newBmpData->bmpWidth = abs(newBmpData->bmpWidth);   
    newBmpData->bmpHeight = abs(newBmpData->bmpHeight);  

    fseek(pFile,BiBitCount,SEEK_CUR);   
    fread(&newBmpData->bmpBiBitCount, 2, 1, pFile);  

    //printf("biBitCount %d\n",newBmpData->bmpBiBitCount);   
    //位图片数据分配空间  
    int BytesPerPixel = newBmpData->bmpBiBitCount / 8;
    int LineLength, TotalLength; 
    LineLength = newBmpData->bmpWidth * BytesPerPixel; // 每行数据长度大致为图象宽度乘以 
    // 每像素的字节数 
    while( LineLength % 4 != 0 ) // 修正LineLength使其为4的倍数 
        ++LineLength; 
    TotalLength = LineLength * newBmpData->bmpHeight; // 数据总长 = 每行长度 * 图象高度

    newBmpData->data = (unsigned char *)malloc(TotalLength * sizeof(unsigned char));

    fseek(pFile,dataOffset,SEEK_SET);  

    fread(newBmpData->data , sizeof(unsigned char), (size_t)(long)TotalLength,  pFile);

    fclose(pFile);
    return newBmpData;
}

 

#define RGBA_TYPE  4

#define RGB_TYPE 3

// 写入bmp文件（24位）

bool SaveBmp(GLint w, GLint h, unsigned char *pdata, char *BmpFileName, bool IsRGBA )
{

#define BMP_Header_Length 54

    unsigned char header[BMP_Header_Length] = {

        0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,

            54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, IsRGBA ? 32 : 24, 0,

            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

            0, 0, 0, 0

    };

    long file_size = (long)w * (long)h * (IsRGBA ? 4 : 3) + 54;

    header[2] = (unsigned char)(file_size &0x000000ff);

    header[3] = (file_size >> 8) & 0x000000ff;

    header[4] = (file_size >> 16) & 0x000000ff;

    header[5] = (file_size >> 24) & 0x000000ff;

    long width = w;

    header[18] = width & 0x000000ff;

    header[19] = (width >> 8) &0x000000ff;

    header[20] = (width >> 16) &0x000000ff;

    header[21] = (width >> 24) &0x000000ff;

    long height = h;

    header[22] = height &0x000000ff;

    header[23] = (height >> 8) &0x000000ff;

    header[24] = (height >> 16) &0x000000ff;

    header[25] = (height >> 24) &0x000000ff;

    FILE *pWritingFile = NULL;

    pWritingFile = fopen(BmpFileName, "wb"); 
    if( pWritingFile == NULL )
        return false;

    fwrite(header, sizeof(unsigned char), 54, pWritingFile);
    int BytesPerPixel = IsRGBA ? 4 : 3;
    int LineLength, TotalLength; 
    LineLength = w * BytesPerPixel; // 每行数据长度大致为图象宽度乘以 
    // 每像素的字节数 
    while( LineLength % 4 != 0 ) // 修正LineLength使其为4的倍数 
        ++LineLength; 
    TotalLength = LineLength * h; // 数据总长 = 每行长度 * 图象高度

    //fwrite(pdata, PixelDataLength, 1, pWritingFile); 
    fwrite(pdata, sizeof(unsigned char), (size_t)(long)TotalLength,  pWritingFile);

    // 释放内存和关闭文件     
    fclose(pWritingFile); 
    return true;
}